'use strict'
class good {
    constructor(){
        this.param = '/:detail'
    }
    __mid(){// 启用token-verify中间件
        return [
          {
            name:'token-verify',
            path:['list','get','post','delete']
          }
        ]
    }
    async list(c) { //请求用户点赞列表
        let userId = c.box.user.userId;
        let sqltext = `SELECT * FROM praise WHERE "userId"=$1`;
        let arr = [];
        let r = await c.service.psql.query(sqltext,[userId]);
        console.log('praise table select result:',r.rows);
        for (let i = 0; i < r.rows.length; i++) {
            console.log('循环执行次数：',i+1)
            console.log(typeof r.rows[i].historyId)
            console.log('r.rows[i].historyId：',r.rows[i].historyId)
            if (r.rows[i].historyId > 0) { 
                console.log('执行了分支查history');
                let sqlSel = `SELECT * FROM history WHERE "historyId"=$1`
                let ret = await c.service.psql.query(sqlSel,[r.rows[i].historyId]);
                arr = arr.concat(ret.rows[0]);
            }
            else if (r.rows[i].mannersId > 0) {
                console.log('执行了分支查manners');
                let sqlSel = `SELECT * FROM manners WHERE "mannersId"=$1`
                let ret = await c.service.psql.query(sqlSel,[r.rows[i].mannersId]);
                arr = arr.concat(ret.rows);
            }
            else if (r.rows[i].poemId > 0) {
                console.log('执行了分支查poem');
                let sqlSel = `SELECT * FROM poem WHERE "poemId"=$1`
                let ret = await c.service.psql.query(sqlSel,[r.rows[i].poemId]);
                arr = arr.concat(ret.rows);
            }
        }
        console.log('arr',arr);
        c.send(JSON.stringify(arr))
    }
    async get(c){ //请求这一文章是否在点赞表里
        let userId = c.box.user.userId;
        let p = c.param.detail.split('&')||[];
        let parmArr = [];
        for (let i = 0; i < p.length; i++) { // 获取参数
            let temp = p[i].split('=');
            let pushData = parseInt(temp[1])
            parmArr.push(pushData);
        }
        // let parmArr = [parseInt(c.param.detail.id||1),parseInt(c.param.detail.flag||0)];
        switch (parmArr[1]) {
            case 0:
                console.log('执行了case0')
                try {
                    let sqltext = `SELECT * FROM praise WHERE "userId"=$1 AND "historyId"=$2`;
                    let r = await c.service.psql.query(sqltext,[userId,parmArr[0]]);
                    if (r.rows.length == 0) {
                        c.send('Not in table');
                    }else {
                        c.send('In table');
                    }
                } catch (err) {
                    c.status(500).send("Database's operations failed!")
                }
                break;
        
            case 1:
                console.log('执行了case1')
                try {
                    console.log("parmArr[0]：",parmArr[0])
                    console.log(typeof parmArr[0])
                    let sqltext = `SELECT * FROM praise WHERE "userId"=$1 AND "mannersId"=$2`;
                    let r = await c.service.psql.query(sqltext,[userId,parmArr[0]]);
                    console.log(r.rows)
                    console.log(r.rows.length)
                    if (r.rows.length == 0) {
                        c.send('Not in table');
                    }else {
                        c.send('In table');
                    }
                } catch (err) {
                    c.status(500).send("Database's operations failed!")
                }
                break;
            case 2:
                console.log('执行了case2')
                try {
                    let sqltext = `SELECT * FROM praise WHERE "userId"=$1 AND "poemId"=$2`;
                    let r = await c.service.psql.query(sqltext,[userId,parmArr[0]]);
                    if (r.rows.length == 0) {
                        c.send('Not in table');
                    }else {
                        c.send('In table');
                    }
                } catch (err) {
                    c.status(500).send("Database's operations failed!")
                }
                break;
        }
    }
    async post(c) { // 点赞
        let userId = c.box.user.userId;
        console.log('userId：',userId);
        console.log("user:",c.box.user);
        // let p = c.param.detail.split('&')||[];
        // let parmArr = [];
        // for (let i = 0; i < p.length; i++) { // 获取参数
        //     let temp = p[i].split('=');
        //     let pushData = temp[1]||1;
        //     parmArr.push(pushData);
        // }
        let parmArr = [parseInt(c.query.id||1),parseInt(c.query.flag||0)];
        console.log('执行了')
        console.log(parmArr)
        switch (parmArr[1]) {
            case 0:
                console.log('执行了case0')
                try {
                    let sqltext = `INSERT INTO `
                                    +`praise("userId","historyId","praiseTime")`
                                    +`VALUES ($1,$2,$3)`;
                    let r = await c.service.psql.query(sqltext,[userId,parmArr[0],new Date().getTime()]);
                    if (r.rowCount != 0) { //对是否点赞成功做判断
                        c.send('Praise successfully!')
                    } else {
                        c.status(500).send('Praise failed!')
                    }
                } catch (err) {
                    c.status(500).send("Database's operations failed!")
                }
                break;
        
            case 1:
                console.log('执行了case1')
                try {
                    let sqltext = `INSERT INTO `
                                    +`praise("userId","mannersId","praiseTime")`
                                    +`VALUES ($1,$2,$3)`;
                    let r = await c.service.psql.query(sqltext,[userId,parmArr[0],new Date().getTime()]);
                    if (r.rowCount != 0) { //对是否点赞成功做判断
                        c.send('Praise successfully!')
                    } else {
                        c.status(500).send('Praise failed!')
                    }
                } catch (err) {
                    c.status(500).send("Database's operations failed!")
                }
                break;
            case 2:
                console.log('执行了case2')
                try {
                    let sqltext = `INSERT INTO `
                                    +`praise("userId","poemId","praiseTime")`
                                    +`VALUES ($1,$2,$3)`;
                    let r = await c.service.psql.query(sqltext,[userId,parmArr[0],new Date().getTime()]);
                    if (r.rowCount != 0) { //对是否点赞成功做判断
                        c.send('Praise successfully!')
                    } else {
                        c.status(500).send('Praise failed!')
                    }
                } catch (err) {
                    c.status(500).send("Database's operations failed!")
                }
                break;
        }
    }
    async delete(c) { //取消点赞   //存在bug，直接走catch分支
        let userId = c.box.user.userId;
        let p = c.param.detail.split('&');
        let parmArr = [];
        console.log('执行了delete good')
        for (let i = 0; i < p.length; i++) { // 获取参数
            let temp = p[i].split('=');
            let pushData = parseInt(temp[1])
            parmArr.push(pushData);
        }
        // let parmArr = [parseInt(c.query.id||1),parseInt(c.query.flag||0)]; //
        switch (parmArr[1]) {
            case 0:
                console.log('执行了case0')
                try {
                    console.log('报错测试1')
                    let sqltext = `DELETE FROM praise WHERE "userId"=$1 AND "historyId"=$2`;
                    console.log("报错测试2")
                    console.log("parmArr[0]:",parmArr[0]);
                    console.log(typeof parmArr[0]);
                    console.log("userId：",userId);
                    console.log(typeof userId);
                    let r = await c.service.psql.query(sqltext,[userId , parmArr[0]] );
                    console.log("报错测试3")
                    console.log('rowCount1：',r.rowCount)
                    if (r.rowCount != 0) { //对是否取消点赞成功做判断
                        console.log('rowCount2：',r.rowCount)
                        c.send('Cancel Praise successfully!')
                    } else {
                        c.send('Cancel Praise failed!')
                    }
                } catch (err) {
                    console.log('执行了catch分支')
                    c.status(500).send("Database's operations failed!")
                }
                break;
        
            case 1:
                try {
                    
                    let sqltext = `DELETE FROM praise WHERE "userId"=$1 AND "mannersId"=$2`;
                    let r = await c.service.psql.query(sqltext,[userId , parmArr[0]] );
                    if (r.rowCount != 0) { //对是否取消点赞成功做判断
                        c.send('Cancel Praise successfully!')
                    } else {
                        c.send('Cancel Praise failed!')
                    }
                } catch (err) {
                    c.status(500).send("Database's operations failed!")
                }
                break;
            case 2:
                try {
                    let sqltext = `DELETE FROM praise WHERE "userId"=$1 AND "poemId"=$2`;
                    let r = await c.service.psql.query(sqltext,[userId,parmArr[0]]);
                    if (r.rowCount != 0) { //对是否取消点赞成功做判断
                        c.send('Cancel Praise successfully!')
                    } else {
                        c.send('Cancel Praise failed!')
                    }
                } catch (err) {
                    c.status(500).send("Database's operations failed!")
                }
                break;
        }
    }
}
module.exports=good;